﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TPA.DataAccess;

namespace TVProgramAnalyst.TimeLine
{
    public partial class frmTagSelector : Form
    {
        public Program Program
        { get; set; }

        public frmTagSelector()
        {
            InitializeComponent();
        }

        private void frmTagSelector_Load(object sender, EventArgs e)
        {
            LoadTagProperty();
        }

        private void LoadTagProperty()
        {
            if (this.Program.Subcategory != null)
            {
                if (global.Default.Program_FormalProgramSubcategoryId == this.Program.SubcategoryId)
                {
                    LoadPropertyUI(global.Default.Program_FormalProgramTagPropertyId);
                }
                else if (global.Default.Program_AdvertisementCategoryId == this.Program.Subcategory.ParentId)
                {
                    LoadPropertyUI(global.Default.Program_AdvertisementTagPropertyID);
                }
            }
        }

        private void LoadPropertyUI(int systemcodeid)
        {
            SystemCode tagProperty = BLLFactory.SystemCodeBLL.Get(systemcodeid);
            foreach (SystemCode sc in tagProperty.SubSystemCodes)
            {

                //flowpanel.Controls.Add(new Label() {Text=sc.Name,Width=flowpanel.Width-30,Height=15 });
                TableLayoutPanel tablepanel = new TableLayoutPanel();
                //tablepanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
                tablepanel.Width = flowpanel.Width - 50;
                tablepanel.Name = sc.Id.ToString();
                tablepanel.ColumnCount = 6;
                tablepanel.Height = 25 * (sc.SubSystemCodes.Count / 6 + (sc.SubSystemCodes.Count % 6 > 0 ? 1 : 0));
                tablepanel.RowCount = 6;
                for (int i = 0; i < 6; i++)
                {
                    tablepanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 100));
                    tablepanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 25));
                }
                IEnumerable<ProgramProperty> properties = null;
                if (this.Program.Properties != null)
                {
                    properties = from p in this.Program.Properties
                                 where (p.PropertyId == sc.Id)
                                 select p;
                }
                foreach (SystemCode scvalue in sc.SubSystemCodes)
                {
                    IEnumerable<ProgramProperty> propertyvalues = null;
                    if (properties != null)
                    {
                        propertyvalues = from p in properties
                                         where p.PropertyValueId == scvalue.Id
                                         select p;
                    }
                    if (propertyvalues != null)
                        tablepanel.Controls.Add(new CheckBox() { Text = scvalue.Name, Tag = scvalue.Id, Checked = (propertyvalues.Count() == 1 ? true : false) });
                    else
                        tablepanel.Controls.Add(new CheckBox() { Text = scvalue.Name, Tag = scvalue.Id });
                }
                //clb.Width = clb.Parent.Width;
                GroupBox gb = new GroupBox() { Text = sc.Name, Width = flowpanel.Width - 30, Height = tablepanel.Height + 30 };
                gb.FlatStyle = FlatStyle.Standard;
                flowpanel.Controls.Add(gb);
                tablepanel.Location = new Point(6, 19);
                gb.Controls.Add(tablepanel);
                //flowpanel.Controls.Add(tablepanel);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Program.Properties.Clear();
            foreach (Control c in flowpanel.Controls)
            {
                foreach (Control checkbox in c.Controls[0].Controls)
                {
                    if(((CheckBox)checkbox).Checked)
                        Program.Properties.Add(new ProgramProperty() { ProgramId = this.Program.Id, PropertyId = int.Parse(c.Controls[0].Name), PropertyValueId = (int)checkbox.Tag });
                }
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
